package com.whg.demo.biz.task;

import org.springframework.data.redis.connection.FutureResult;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

/**
 * @Author whg
 * @create 2022/12/9 10:02
 * @description：
 */
@Component
//@Async
public class TestAsync {

    public void task1() throws InterruptedException {
        long begin = System.currentTimeMillis();
        TimeUnit.SECONDS.sleep(1);
        long end = System.currentTimeMillis();
        System.out.println("任务一耗时：" + (end - begin));
    }

    public void task2() throws InterruptedException {
        long begin = System.currentTimeMillis();
        TimeUnit.SECONDS.sleep(2);
        long end = System.currentTimeMillis();
        System.out.println("任务二耗时：" + (end - begin));
    }

    public void task3() throws InterruptedException {
        long begin = System.currentTimeMillis();
        TimeUnit.SECONDS.sleep(3);
        long end = System.currentTimeMillis();
        System.out.println("任务三耗时：" + (end - begin));
    }

    public Future<String> task4() throws InterruptedException {
        long begin = System.currentTimeMillis();
        TimeUnit.SECONDS.sleep(1);
        long end = System.currentTimeMillis();
        System.out.println("任务四耗时：" + (end - begin));
        return new AsyncResult<>("任务四");
    }

    public Future<String> task5() throws InterruptedException {
        long begin = System.currentTimeMillis();
        TimeUnit.SECONDS.sleep(2);
        long end = System.currentTimeMillis();
        System.out.println("任务五耗时：" + (end - begin));
        return new AsyncResult<>("任务五");
    }

    @Async
    public Future<String> task6() throws InterruptedException {
        long begin = System.currentTimeMillis();
        TimeUnit.SECONDS.sleep(3);
        long end = System.currentTimeMillis();
        System.out.println("任务六耗时：" + (end - begin));
        return new AsyncResult<>("任务六");
    }
}
